package com.ttctracker.splashscreen;

import java.net.URL;
import java.util.HashMap;

import org.xml.sax.InputSource;

import com.ttctracker.Main;
import com.ttctracker.R;
import com.ttctracker.splashscreen.SplashScreenActivity;
import com.ttctracker.util.MyApp;
import com.ttctracker.util.SqlAdapter;

import com.ttctracker.xmlparser.RouteTagsHandler;
import com.ttctracker.xmlparser.SAXParserHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.DatabaseUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class SplashScreenActivity extends Activity{

	
	private final int welcomeScreenDisplay=1500;
	private MyApp myApplication;
	private TextView initLabel;
	private SqlAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen);
		initLabel=(TextView)findViewById(R.id.txtInitLabel);
		
		myApplication=((MyApp)getApplicationContext());
		if (MyApp.DEBUG) {
			deleteDatabase(MyApp.getDBName());
		}
		new StartupTask().execute();
		
		
	}
	
	private void init_complete(HashMap<String, String> route_tags) {
		if (route_tags!=null) {
				myApplication.setRouteTags(route_tags);
			myApplication.setupRouteArrayAdapter();
			startActivity(new Intent(SplashScreenActivity.this,
					Main.class));
		}
		
		finish();
		 
	}
	
	
	public class StartupTask extends AsyncTask<String, Integer, HashMap<String, String>>{

		private static final String ROUTE_LIST_STRING="http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=ttc";
		private SAXParserHelper helper;
		private RouteTagsHandler routeTagsHandler;
		private ProgressDialog dialog;
		private int maxProgress=0;
		@Override
		protected void onPostExecute(HashMap<String, String> result) {
			init_complete(result);
		}

		@Override
		protected void onPreExecute() {
			adapter=new SqlAdapter(SplashScreenActivity.this);
			adapter.openDB();
			if (!adapter.gotRouteTableExist()) {
				dialog=new ProgressDialog(SplashScreenActivity.this);
				dialog.setTitle("First time Initializing...");
				dialog.setMessage("First time initilizing the app will take a while, please do not close it.");
				dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				dialog.show();
			}
		}
		
		public void updateProgress(int progress) {
			publishProgress(progress);
		}
		
		@Override
		protected void onProgressUpdate(Integer... values) {
			if (values[0]==-1) {
				dialog.setMax(maxProgress);
			}else {
				int progress=values[0];
				dialog.setProgress(progress);
			}
			//super.onProgressUpdate(values);
		}
		
		@Override
		protected HashMap<String, String> doInBackground(String... arg0) {
			try {
								
				HashMap<String, String> route_tags=null;
				
				try {
					//adapter.openDB();
					if (!adapter.gotRouteTableExist()) {
						
						//initLabel.setText("Initializing...");
						// perform the synchronous parse
						helper=new SAXParserHelper(new URL(ROUTE_LIST_STRING).openStream(), RouteTagsHandler.class);
						helper.setup_parser();
						
						helper.parseXml();
						routeTagsHandler=(RouteTagsHandler) helper.getHandler();
						// get the results - should be a fully populated route data
						// or null on error
						route_tags=routeTagsHandler.getRoute_tags();
						maxProgress=route_tags.size();
						publishProgress(-1);
						adapter.insert_route(route_tags,this);
						
						if (dialog!=null) {
							
							dialog.dismiss();
						}
						
					}else {
						route_tags=adapter.selectRouteTags();
					}
					adapter.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return route_tags;
			} catch (Exception e) {
				// if you have a problem, simply return null
				e.printStackTrace();
				return null;
			}
		}

	}

}
